package com.nl.controller.web.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.model.Response;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.web.bean.SmsMgrInfo;
import com.nl.controller.web.bean.SmsRule;
import com.nl.controller.web.bean.SmsUsers;
import com.nl.controller.web.mapper.UtilMapper;
import com.nl.controller.web.service.SmsUserService;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("user")
@Slf4j
public class SmsUserConroller {
    @Autowired
    private SmsUserService smsUserService;
    @Autowired
    private UtilMapper utilMapper;
    
    @ApiOperation(value = "用户管理模板", notes = "分页查询用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, dataType = "String"),
            @ApiImplicitParam(name = "msisdn", value = "手机号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "name", value = "用户姓名", required = true, dataType = "String"),
            @ApiImplicitParam(name = "duty", value = "级别", required = true, dataType = "String"),
            @ApiImplicitParam(name = "cityId", value = "地市", required = true, dataType = "String"),
            @ApiImplicitParam(name = "countyId", value = "区县", required = true, dataType = "String"),
            @ApiImplicitParam(name = "isCheck", value = "审核人", required = true, dataType = "String"),
    })
    @PostMapping("find")
    public Response findUser(
            @RequestParam(value = "page", required = true) int page,
            @RequestParam(value = "limit", required = true) int limit,
            @RequestParam(value = "msisdn", required = false) String msisdn,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "duty", required = false) String duty,
            @RequestParam(value = "cityId", required = false) String cityId,
            @RequestParam(value = "countyId", required = false) String countyId,
            @RequestParam(value = "isCheck", required = false) String isCheck,
            @RequestParam Map<String, Object> paramMap,HttpSession session
    ) {
    	if ( duty == null) {
            throw new SmsException("请选择查询条件，开始查询");
        } else {
        	if("0".equals(duty)){cityId="25";}
        	SmsUsers user = (SmsUsers) session.getAttribute("user");
        	if("2".equals(user.getDuty())){
        		if("1".equals(duty)){
        		countyId="";	
        		}	
        	}
        PageInfo<SmsUsers> pageInfo = smsUserService.findUser(page, limit, msisdn, name, duty, cityId, countyId, isCheck);
        log.info("用户查询：{}", paramMap.toString());
        log.debug("用户查询统计返回数据：{}", pageInfo.getList().toString());
        return ResponseUtil.sucess(pageInfo.getList(), pageInfo.getTotal());
        }
    }
    
    @RequestMapping("findUserBymsisdn")
    public Response findUserBymsisdn(
    		@RequestParam Map<String, Object> paramMap,
            @RequestParam(value = "copymsisdn", required = false) String copymsisdn,
            @RequestParam(value = "copyname", required = false) String copyname,
            HttpSession session
    ) {
    	if (copymsisdn == null && copyname == null) {
    		return ResponseUtil.error(-1, "请输入正确的用户名或手机号");
		}
        List<SmsUsers> list = smsUserService.findUserBymsisdn( copymsisdn, copyname);
        list = smsUserService.setMgrName(list);
        log.info("用户查询：{}", copymsisdn+copyname);
        if ( !list.isEmpty() && list.size() ==1 ) {
        	log.info("用户查询统计返回数据：{}", list.get(0).toString());
        	if ("3".equals(list.get(0).getDuty())) {
        		SmsUsers users = (SmsUsers) session.getAttribute("user");
        		if ("0".equals(users.getDuty()) ) {
        			return ResponseUtil.sucess(list.get(0));
				}else if ("1".equals(users.getDuty())  && users.getCityId().equals(list.get(0).getCityId())) {
					return ResponseUtil.sucess(list.get(0));
				}else if ("2".equals(users.getDuty()) && users.getCountyId().equals(list.get(0).getCityId())) {
					return ResponseUtil.sucess(list.get(0));
				}else {
					return ResponseUtil.error(-1, "请输入管辖内项目管理员用户名或手机号");
				}
        		
			}else {
				return ResponseUtil.error(-1, "请输入正确的项目管理员用户名或手机号");
			}
		}else {
			return ResponseUtil.error(-1, "请输入正确的项目管理员用户名或手机号");
		}
        
        
    }
    @RequestMapping("checkMgrName")
    public Response   checkMgrName(
    		@RequestParam Map<String, Object> paramMap,
            @RequestParam(value = "cityId", required = false) String cityId,
            @RequestParam(value = "countyId", required = false) String countyId,
            @RequestParam(value = "mgrName", required = false) String mgrName
    ) {
    	log.info("用户管理项目简称查询参数：{}", paramMap);
    	List<SmsMgrInfo> list = smsUserService.checkMgrName(cityId, countyId, mgrName);
    	if (list.size()>0) {
    		return ResponseUtil.sucess(list);
		}else {
			return ResponseUtil.error(-1, "无项目,请输入正确的项目简称");
		}
    	
    }
    
    @PostMapping("findduty")
    public Response findduty(
    		HttpSession session
    ) {
    	SmsUsers users = (SmsUsers) session.getAttribute("user");
    	String usercountyId = users.getCountyId();
    	String countyName="全部";
    	if (usercountyId == null || "".equals(usercountyId)) {
    		usercountyId = "";
    		users.setCountyId(usercountyId);
		}else {
			countyName  = utilMapper.getCountyName(usercountyId);
		}
    	users.setCountyName(countyName);
    	return ResponseUtil.sucess(users);
    }
    
    @ApiOperation(value = "用户管理模板", notes = "查询角色信息")
    @PostMapping("findRule")
    public Response<SmsRule> findOperatorByCityId( @RequestParam(value = "duty") String duty) {
        List<SmsRule> list = smsUserService.findRule(duty);
        log.info("查询角色信息：{}");
        log.debug("查询角色信息返回参数：{}",list.size());
        return ResponseUtil.sucess(list);
    }


    @ApiOperation(value = "用户管理模板", notes = "添加用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "msisdn", value = "手机号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "userName", value = "用户姓名", required = true, dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "duty", value = "等级", required = true, dataType = "String"),
            @ApiImplicitParam(name = "cityId", value = "城市编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "mgrId", value = "项目编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "roleId", value = "角色编号", required = true, dataType = "String")
    })
    @PostMapping("add")
    public int addUser(
            @RequestParam(value = "msisdn") String msisdn,
            @RequestParam(value = "userName") String userName,
            @RequestParam(value = "password") String password,
            @RequestParam(value = "duty") String duty,
            @RequestParam(value = "cityId") String cityId,
            @RequestParam(value = "mgrId") String mgrId,
            @RequestParam(value = "roleId") String roleId,
            @RequestParam(value = "isCheck") String isCheck,
            @RequestParam(value = "countyId") String countyId
            
    ) {
    	if ( !"0".equals(duty) && !"1".equals(duty)) {
    		isCheck ="1";
		}
        return smsUserService.addUser(msisdn, userName, password, duty, cityId, mgrId,roleId,isCheck,countyId);
    }

    @ApiOperation(value = "用户修改模板", notes = "查询单个用户信息")
    @PostMapping("findUserByUserId")
    @ApiImplicitParam(name = "userId", value = "userId", required = true, dataType = "String")
    public SmsUsers findUserByUserId(
            @RequestParam(value = "userId") String userId) {
        SmsUsers smsUsers = new SmsUsers();
        smsUsers = smsUserService.findUserByUserId(userId);
        log.info("查询单个用户参数：{}", userId);
        log.debug("查询单个用户返回数据：{}", smsUsers.toString());
        return smsUsers;
    }

    @ApiOperation(value = "用户修改模板", notes = "修改用户的信息")
    @PostMapping("modify")
    public int modifyUser(
            @RequestParam(value = "userId") String userId,
            @RequestParam(value = "msisdn") String msisdn,
            @RequestParam(value = "userName") String userName,
            @RequestParam(value = "password") String password,
            @RequestParam(value = "duty") String duty,
            @RequestParam(value = "cityId") String cityId,
            @RequestParam(value = "mgrId") String mgrId,
            @RequestParam(value = "roleId") String roleId,
            @RequestParam(value = "isCheck") String isCheck,
            @RequestParam(value = "countyId") String countyId,
            @RequestParam Map<String, Object> paramMap
    ) {
    	if ( !"0".equals(duty) && !"1".equals(duty)) {
    		isCheck ="1";
    	}
        int result = smsUserService.modifyUser(userId,msisdn, userName,password, duty, cityId, mgrId, roleId,isCheck,countyId);
        log.info("用户修改参数：{}", paramMap.toString());
        log.debug("用户修改返回数据：{}", result);
        return result;
    }

    @ApiOperation(value = "用户删除模板", notes = "删除用户的信息")
    @PostMapping("remove")
    @ApiImplicitParams({
    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String"),
    @ApiImplicitParam(name = "cityId", value = "地市id", required = true, dataType = "String"),
    @ApiImplicitParam(name = "duty", value = "用户权限", required = true, dataType = "String")
    })
    public int deleteUser(@RequestParam(value = "userId") String userId,
    		@RequestParam(value = "cityId") String cityId,
    		@RequestParam(value = "duty") String duty
    		) {
        int result= smsUserService.deleteUser(userId,cityId,duty);
        log.info("用户删除参数：{}", userId);
        log.debug("用户删除返回数据：{}", result);
        return result;
    }  
    
    @ApiOperation(value = "查询用户历史表", notes = "查询用户历史表")
    @PostMapping("findHis")
    public Response findHisUser(
            @RequestParam(value = "seqId", required = true) String seqId
    ) {
    	SmsUsers smsHisUsers;
    	smsHisUsers = smsUserService.findHisUser(seqId);
        log.info("查询用户历史：{}", smsHisUsers);
        return ResponseUtil.sucess(smsHisUsers);        
    }
}
